<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsNum">
      <el-input v-model="dataForm.goodsNum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsAttr">
      <el-input v-model="dataForm.goodsAttr" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isReal">
      <el-input v-model="dataForm.isReal" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isSale">
      <el-input v-model="dataForm.isSale" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="marketPrice">
      <el-input v-model="dataForm.marketPrice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="realPrice">
      <el-input v-model="dataForm.realPrice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sumPrice">
      <el-input v-model="dataForm.sumPrice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="thumbUrl">
      <el-input v-model="dataForm.thumbUrl" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="plusClass">
      <el-input v-model="dataForm.plusClass" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="decrClass">
      <el-input v-model="dataForm.decrClass" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="bookingTime">
      <el-input v-model="dataForm.bookingTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          goodsId: '',
          goodsName: '',
          goodsNum: '',
          goodsAttr: '',
          isReal: '',
          isSale: '',
          marketPrice: '',
          realPrice: '',
          status: '',
          sumPrice: '',
          thumbUrl: '',
          createTime: '',
          updateTime: '',
          plusClass: '',
          decrClass: '',
          bookingTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsNum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsAttr: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isReal: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isSale: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          marketPrice: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          realPrice: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sumPrice: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          thumbUrl: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          plusClass: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          decrClass: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          bookingTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/school/cart/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.cart.userId
                this.dataForm.goodsId = data.cart.goodsId
                this.dataForm.goodsName = data.cart.goodsName
                this.dataForm.goodsNum = data.cart.goodsNum
                this.dataForm.goodsAttr = data.cart.goodsAttr
                this.dataForm.isReal = data.cart.isReal
                this.dataForm.isSale = data.cart.isSale
                this.dataForm.marketPrice = data.cart.marketPrice
                this.dataForm.realPrice = data.cart.realPrice
                this.dataForm.status = data.cart.status
                this.dataForm.sumPrice = data.cart.sumPrice
                this.dataForm.thumbUrl = data.cart.thumbUrl
                this.dataForm.createTime = data.cart.createTime
                this.dataForm.updateTime = data.cart.updateTime
                this.dataForm.plusClass = data.cart.plusClass
                this.dataForm.decrClass = data.cart.decrClass
                this.dataForm.bookingTime = data.cart.bookingTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/school/cart/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'goodsNum': this.dataForm.goodsNum,
                'goodsAttr': this.dataForm.goodsAttr,
                'isReal': this.dataForm.isReal,
                'isSale': this.dataForm.isSale,
                'marketPrice': this.dataForm.marketPrice,
                'realPrice': this.dataForm.realPrice,
                'status': this.dataForm.status,
                'sumPrice': this.dataForm.sumPrice,
                'thumbUrl': this.dataForm.thumbUrl,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'plusClass': this.dataForm.plusClass,
                'decrClass': this.dataForm.decrClass,
                'bookingTime': this.dataForm.bookingTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
